package com.hzgj.bcl.soa.util;


/**
 * Class that represents a response from a communication
 */
public class Rsp<T> {
    /**
     * Flag that represents whether the response was received
     */
    protected boolean received;
    /**
     * Flag that represents whether the sender of the response was suspected
     */
    protected boolean suspected;
    /**
     * If true, the sender (below) could not be reached, e.g. if a site was down (only used by RELAY2)
     */
    protected boolean unreachable;
    /**
     * The value from the response
     */
    protected T retval;

    /**
     * If there was an exception, this field will contain it
     */
    protected Throwable exception;


    public T getValue() {
        return retval;
    }

    public void setValue(T val) {
        this.retval = val;
        setReceived();
        exception = null;
    }

    public boolean hasException() {
        return exception != null;
    }

    public Throwable getException() {
        return exception;
    }

    public void setException(Throwable t) {
        if (t != null) {
            this.exception = t;
            setReceived();
            retval = null;
        }
    }

    public boolean wasReceived() {
        return received;
    }

    public void setReceived() {
        received = true;
    }

    public boolean wasSuspected() {
        return suspected;
    }

    public boolean setSuspected() {
        boolean changed = !suspected;
        suspected = true;
        return changed;
    }

    public boolean wasUnreachable() {
        return unreachable;
    }

    public boolean setUnreachable() {
        boolean changed = !unreachable;
        unreachable = true;
        return changed;
    }


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (retval != null) {
            sb.append(", retval=").append(retval);
        }
        if (exception != null) {
            sb.append(", exception=").append(exception);
        }
        sb.append(", received=").append(received).append(", suspected=").append(suspected);
        if (unreachable) {
            sb.append(" (unreachable)");
        }
        return sb.toString();
    }
}

